package com.kx.game.center.serverDistribution;

import com.alibaba.fastjson.JSONObject;
import com.kx.game.center.global.GSServices;
import com.kx.game.center.redis.RedisConstant;
import com.redis.core.manager.RedisManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManualDistribution implements ServerDistributionInterface{

    @Override
    public JSONObject execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        RedisManager redisManager = GSServices.redisServer.getRedisClient();
        String serverKey = String.valueOf(GSServices.serverConfig.getServerId());
        int num = Integer.parseInt(redisManager.hashGet(
                RedisConstant.SERVER_CREATE_NUM, serverKey, String.class));
        if (GSServices.serverConfig.getServerPlayerNum() > num &&
                Integer.parseInt(serverKey) != GSServices.serverConfig.getFriendServerId()){
            JSONObject jsonObject = GSServices.localManager.getGameServerInfo(serverKey);
            return GSServices.localManager.getJson(
                    ResponseState.SUCCESS,String.valueOf(jsonObject.get("ip")),String.valueOf(jsonObject.get("gamePort")));
        }else {
            return GSServices.localManager.getJson(ResponseState.SERVER_NUM_FULL,null, null);
        }
    }
}
